-- Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_Insert]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_Update]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_InsertUpdate]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_InsertUpdate]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_Delete]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_DeleteDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_DeleteDynamic]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_Load]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_Load]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_SelectDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_SelectDynamic]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_SelectAll]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_SelectAll]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_SelectBy_ProjectID]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_SelectBy_ProjectID]

IF OBJECT_ID(N'[dbo].[p_ACTP_TaskGroup_DeleteBy_ProjectID]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_TaskGroup_DeleteBy_ProjectID]


GO


------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_Insert]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_Insert]
	@ModuleID uniqueidentifier,
	@ProjectID int,
	@Title nvarchar(128),
	@Description nvarchar(1024),
	@ID int OUTPUT
AS

INSERT INTO [dbo].[t_ACTP_TaskGroups]
(
	[ModuleID],
	[ProjectID],
	[Title],
	[Description]
)
VALUES 
(
	@ModuleID,
	@ProjectID,
	@Title,
	@Description
)

SET @ID = SCOPE_IDENTITY()

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_Update]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_Update]
	@ID int,
	@ModuleID uniqueidentifier,
	@ProjectID int,
	@Title nvarchar(128),
	@Description nvarchar(1024)
AS

UPDATE
	[dbo].[t_ACTP_TaskGroups]
SET
	[ModuleID] = @ModuleID,
	[ProjectID] = @ProjectID,
	[Title] = @Title,
	[Description] = @Description
WHERE
	[ID] = @ID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_InsertUpdate]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_InsertUpdate]
	@ID int,
	@ModuleID uniqueidentifier,
	@ProjectID int,
	@Title nvarchar(128),
	@Description nvarchar(1024)
AS
IF EXISTS(SELECT [ID] FROM [dbo].[t_ACTP_TaskGroups] WHERE [ID] = @ID)
	BEGIN
		UPDATE
			[dbo].[t_ACTP_TaskGroups] 
		SET
			[ModuleID] = @ModuleID,
			[ProjectID] = @ProjectID,
			[Title] = @Title,
			[Description] = @Description
		WHERE
			[ID] = @ID
	END
ELSE
	BEGIN
		
		INSERT INTO [dbo].[t_ACTP_TaskGroups]
		(
			[ModuleID],
			[ProjectID],
			[Title],
			[Description]
		)
		VALUES 
		(
			@ModuleID,
			@ProjectID,
			@Title,
			@Description
		)		
	END
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_Delete]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_Delete]
	@ID int
AS

DELETE FROM 
	[dbo].[t_ACTP_TaskGroups]
WHERE
	[ID] = @ID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_DeleteBy_ProjectID]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_DeleteBy_ProjectID]
	@ProjectID int
AS

DELETE FROM [dbo].[t_ACTP_TaskGroups]
WHERE
	[ProjectID] = @ProjectID

GO

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_DeleteDynamic]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_DeleteDynamic]
	@WhereCondition NVARCHAR(500)
AS

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 'DELETE FROM [dbo].[t_ACTP_TaskGroups] WHERE ' + @WhereCondition

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_Load]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_Load]
	@ID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[ProjectID],
	[Title],
	[Description]
FROM
	[dbo].[t_ACTP_TaskGroups]
WHERE
	[ID] = @ID
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_SelectBy_ProjectID]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_SelectBy_ProjectID]
	@ProjectID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[ProjectID],
	[Title],
	[Description]
FROM
	[dbo].[t_ACTP_TaskGroups]
WHERE
	[ProjectID] = @ProjectID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_SelectDynamic]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_SelectDynamic]
	@WhereCondition NVARCHAR(500),
	@OrderByExpression NVARCHAR(250) = NULL
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 
'SELECT 
	[ID],
	[ModuleID],
	[ProjectID],
	[Title],
	[Description]
FROM [dbo].[t_ACTP_TaskGroups] 
WHERE ' + @WhereCondition

IF @OrderByExpression IS NOT NULL AND LEN(@OrderByExpression) > 0
BEGIN
	SET @SQL = @SQL + ' ORDER BY ' + @OrderByExpression
END

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_TaskGroup_SelectAll]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_TaskGroup_SelectAll]
	@ModuleID UNIQUEIDENTIFIER
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[ProjectID],
	[Title],
	[Description]
FROM
	[dbo].[t_ACTP_TaskGroups]	
WHERE 
	(ModuleID = @ModuleID)

GO

